package com.ihealthway.all.groupbuy.services;

import com.ihealthway.all.groupbuy.vo.SystemDict;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.*;

/**
 * System dict service
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean(fields = {"dao"})
public class SysDictService extends BaseService<SystemDict> {

    private static final Log logger = Logs.get();

    public Map<String, List<SystemDict>> getSysDictOnFront() {

        Map<String, List<SystemDict>> result = new HashMap<>();

        Sql sql = Sqls.create("SELECT sd_id,parent_sd_id,dict_name FROM gb_system_dict " +
                "WHERE parent_sd_id NOT IN (-1,1) AND data_status <> 1 " +
                "GROUP BY sd_id,parent_sd_id,dict_name ORDER BY sd_id,parent_sd_id");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SystemDict.class));

        List<SystemDict> data = dao().execute(sql).getList(SystemDict.class);
        List<SystemDict> age = new ArrayList<>();
        List<SystemDict> height = new ArrayList<>();
        List<SystemDict> weight = new ArrayList<>();
        data.forEach(sd -> {
            switch (sd.getParentDictId()) {
                case 2:
                    age.add(sd);
                    break;
                case 3:
                    height.add(sd);
                    break;
                case 4:
                    weight.add(sd);
                    break;
            }
        });

        result.put("2", age);
        result.put("3", height);
        result.put("4", weight);

        return result;
    }

    public List<SystemDict> getFirstLevelDict() {

        List<SystemDict> dicts = new ArrayList<>();

        Cnd cnd = Cnd.where("parent_sd_id", "IN", "-1, 1").and("data_status", "=","0");
        try {
            dicts.addAll(query(cnd));
        }catch (Exception e) {
            logger.error("Get system parent dict failed.", e);
        }

        return dicts;
    }

    public boolean checkDitVal(String dictVal) {

        boolean exist = false;

        try {
            SystemDict dict = get("dict_value", dictVal);
            exist = (dict!=null);
        } catch (Exception e) {
            logger.error("Check exist system dict failed.", e);
        }

        return exist;
    }

    public boolean add(SystemDict systemDict) {

        boolean added = false;

        try {
            systemDict.setDataStatus("0");
            systemDict.setCreateTime(new Date());
            SystemDict tmp = dao().fastInsert(systemDict);
            added = tmp!=null;
        } catch (Exception e) {
            logger.error("Add new system dict failed.", e);
        }

        return added;
    }

    public boolean update(SystemDict sysDict) {

        boolean updated = false;

        sysDict.setUpdateTime(new Date());
        try {
            int effectRows = dao().updateIgnoreNull(sysDict);
            logger.infof("Update system dict effect rows: %s", effectRows);
            updated =  (effectRows != 0);
        } catch (Exception e) {
            logger.error("Update system dict failed.", e);
        }

        return updated;
    }

    public Map<String, Object> getGridData(int id, int page, int rows) {

        if (id < 1) {
            id = 1;
        }

        Criteria cri = Cnd.cri();
        cri.where().and("parent_sd_id", "=", id);
        cri.getOrderBy().asc("order_no");

        Map<String, Object> data = new HashMap<>();
        try {
            data.putAll(easyuiDGPager(page,rows, cri));
        } catch (Exception e) {
            logger.error("Find system dict grid data failed.", e);
        }

        return data;
    }
}
